package com.wsx.ones.cloud.hello;

import com.wsx.ones.cloud.AbstractBaseController;
import com.wsx.ones.cloud.config.DataConfig;
import com.wsx.ones.cloud.redis.RedisService;
import com.wsx.ones.cloud.util.RedisFinalString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Created by wangshuaixin on 17/5/4.
 */
@RestController
@RequestMapping("/user")
public class HelloController extends AbstractBaseController {

    @Autowired
    private DataConfig dataConfig;

    @Autowired
    private RedisService redisService;


    @RequestMapping(
            value = "/hello/get/{name}",
            method = {RequestMethod.GET}
    )
    @ResponseBody
    public String hello(@PathVariable String name) {
        return "hello:" + name;
    }

    @RequestMapping(
            value = "/config/get",
            method = {RequestMethod.GET}
    )
    public String config() {

        StringBuilder builder = new StringBuilder(dataConfig.getUrl());
        builder.append(dataConfig.getDriverClassName());
        builder.append(dataConfig.getUsername());
        builder.append(dataConfig.getPassword());

        return builder.toString();
    }

    @RequestMapping(
            value = "/redis/test",
            method = {RequestMethod.GET}
    )
    public String redis(@RequestParam("name") String name) {
        String value = redisService.get(name, RedisFinalString.DB_2);
        if (StringUtils.isEmpty(value)) {
            value = "wang shuai xin";
            redisService.set(name, RedisFinalString.DB_2, value);
        }
        String v = redisService.get(name, RedisFinalString.DB_2);
        String u = redisService.get(name, RedisFinalString.DB_1);
        return value;
    }

    @RequestMapping(
            value = "/hello/v1",
            method = {RequestMethod.POST}
    )
    @ResponseBody
    public String hello2(List<Map<String, String>> list) {
        if (null != list) {

        }
        return "hello:" ;
    }



}
